﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Default.aspx.cs" company="CodePlex">
//   Copyright © CodePlex 2012
// </copyright>
// <summary>
//   Defines the _Default type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Example.Web
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Web.UI;

    using Example.Business;
    using Example.Common;

    /// <summary>
    /// Code behind for the default page.
    /// </summary>
    public partial class Default : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.DisplayContacts();
            }
        }

        /// <summary>
        /// Handles the Click event of the btnInsert control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonInsertClick(object sender, EventArgs e)
        {
            var controller = new DefaultController();
            var newContact = new Contact
                {
                    Number = this.gvContactsObjects.Rows.Count + 1,
                    Id = "testItem",
                    Address = "123 Test Item Ln.",
                    City = "San Antoino",
                    Email = DateTime.Now.ToString(CultureInfo.InvariantCulture),
                    Name = "John Doe",
                    Phone = "123.456.7890",
                    State = "TX",
                    ZipCode = "78251"
                };

            controller.CreateNewContact(newContact);

            this.DisplayContacts();
        }

        /// <summary>
        /// Handles the Click event of the btnInsert control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonBulkInsertClick(object sender, EventArgs e)
        {
            var controller = new DefaultController();
            var newContacts = new List<Contact>();
            var number = this.gvContactsObjects.Rows.Count + 1;

            for (var i = 0; i < 5; i++)
            {
                var newContact = new Contact
                {
                    Number = number + i,
                    Id = "testItem",
                    Address = "123 Test Item Ln.",
                    City = "San Antoino",
                    Email = DateTime.Now.ToString(CultureInfo.InvariantCulture),
                    Name = "John Doe",
                    Phone = "123.456.7890",
                    State = "TX",
                    ZipCode = "78251"
                };

                newContacts.Add(newContact);
            }

            controller.BulkCreateNewContact(newContacts);

            this.DisplayContacts();
        }

        /// <summary>
        /// Handles the Click event of the btnUpdate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonUpdateClick(object sender, EventArgs e)
        {
            var controller = new DefaultController();
            var contact = controller.FetchAllAsObjects().FirstOrDefault(c => c.Id == "testItem");

            if (contact != null)
            {
                contact.Email = DateTime.Now.ToString(CultureInfo.InvariantCulture);

                controller.UpdateContact(contact);
            }

            this.DisplayContacts();
        }

        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonDeleteClick(object sender, EventArgs e)
        {
            var controller = new DefaultController();
            var contact = controller.FetchAllAsObjects().FirstOrDefault(c => c.Id == "testItem");

            controller.RemoveContact(contact);

            this.DisplayContacts();
        }

        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonDeleteAllInsertedClick(object sender, EventArgs e)
        {
            var controller = new DefaultController();
            var contacts = controller.FetchAllAsObjects().Where(c => c.Id == "testItem");

            controller.BulkRemoveContacts(contacts);

            this.DisplayContacts();
        }

        /// <summary>
        /// Displays the contacts.
        /// </summary>
        private void DisplayContacts()
        {
            var businessObject = new DefaultController();

            this.gvContactsTable.DataSource = businessObject.FetchAllContacts();
            this.gvContactsTable.DataBind();

            this.gvContactsObjects.DataSource = businessObject.FetchAllAsObjects();
            this.gvContactsObjects.DataBind();
        }
    }
}
